package com.user.service.impl;

import java.util.Iterator;
import java.util.List;
import com.user.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.user.mapper.*;
import com.user.service.*;

/*业务层：UserService接口的实现类*/
@Service
public class UserServiceImpl implements UserService {
//	从IOC容器中注入userMapper对象
	@Autowired
	private UserMapper userMapper;

//	查询所有数据
	public List<User> findAll() {
		// TODO Auto-generated method stub
		return findAll(null);
	}

//	根据whereWrap条件查询符合条件的数据
	public List<User> findAll(User whereWrap) {
		// TODO Auto-generated method stub
		return findAll(whereWrap, true);
	}

//	根据whereWrap条件和查询模式查询符合条件的数据
	public List<User> findAll(User whereWrap, boolean isDistance) {
		// TODO Auto-generated method stub
		List<User> list = userMapper.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<User> iterator = list.iterator();
			while (iterator.hasNext()) {
				User user = (User) iterator.next();
				if (whereWrap.getUsername() != null&&(!(whereWrap.getUsername()+"").equals(""))) {
					if (isDistance) {
						if (!(user.getUsername() + "").equals(whereWrap.getUsername() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(user.getUsername() + "").contains(whereWrap.getUsername() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPassword() != null&&(!(whereWrap.getPassword()+"").equals(""))) {
					if (isDistance) {
						if (!(user.getPassword() + "").equals(whereWrap.getPassword() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(user.getPassword() + "").contains(whereWrap.getPassword() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		return list;
	}

//	根据主键查询符合条件的数据
	public User findById(String username) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(username);
	}

//	插入一条user数据
	public int insert(User user) {
		// TODO Auto-generated method stub
		return userMapper.insertSelective(user);
	}

//	根据主键更新一条user数据
	public int updateById(User user) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKey(user);
	}

//	根据主键选择性更新一条user数据
	public int updateByIdSelective(User user) {
		// TODO Auto-generated method stub
		User dbUser = findById(user.getUsername());
		if (user.getUsername()==null) {
			user.setUsername(dbUser.getUsername());
		}
		if (user.getPassword()==null) {
			user.setPassword(dbUser.getPassword());
		}
		return userMapper.updateByPrimaryKey(user);
	}

//	根据主键删除一条user数据
	public int deleteById(String username) {
		// TODO Auto-generated method stub
		return userMapper.deleteByPrimaryKey(username);
	}

//	根据whereWrap条件删除符合条件的数据
	public int delete(User whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap, true);
	}

//	根据whereWrap条件和查询模式删除符合条件的数据
	public int delete(User whereWrap, boolean isDistance) {
		// TODO Auto-generated method stub
		List<User> list = findAll(whereWrap, isDistance);
		for (User user : list) {
			userMapper.deleteByPrimaryKey(user.getUsername());
		}
		return 0;
	}

//	根据主键集合批量删除user数据
	public int deleteByIds(List<String> list) {
		// TODO Auto-generated method stub
		for (String username : list) {
			deleteById(username);
		}
		return 0;
	}
}

